﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
using UmcProvider.Shared;
using UmcProvider.PropertiesDesign;
using System.Xml;
using System.ComponentModel.Design;

using UmcControls.DesignInterface;

namespace Wicresoft.Shell.Package.AppExp.ToolboxControl
{
    [Designer(typeof(UmcListDesigner<IListBox>))]
    [DisplayName("ListBox")]
    [Description("Umc custom ListBox.")]
    [ToolboxItem(true)]
    [ToolboxBitmap(typeof(System.Windows.Forms.ListBox), "ListBox.bmp")]
    [UmcContorlGoupName("UMC Common Controls")]
    public class ListBox : System.Windows.Forms.ListBox, IUmcPropertyStore
    {
        //store the properties parse from code which is not surpported by designer
        //also store the properties which is set value using script variable
        //users should operator the dic through the IUmcPropertyStore
        private Dictionary<string, string> DesignViewUnSurpportProperty = new Dictionary<string, string>();

        

        #region IUmcPropertyStore Members

        public void AddUmcProperty(string property, string value)
        {
            DesignViewUnSurpportProperty.Add(property, value);
        }

        public void RemoveUmcProperty(string property)
        {
            DesignViewUnSurpportProperty.Remove(property);
        }

        public void RemoveAllUmcProperties()
        {
            DesignViewUnSurpportProperty.Clear();
        }

        public string GetUmcProperty(string property)
        {
            if (DesignViewUnSurpportProperty.ContainsKey(property))
            {
                return DesignViewUnSurpportProperty[property];
            }
            else
            {
                return null;
            }
        }

        public List<KeyValuePair<string, string>> GetUmcPropertyList()
        {
            return DesignViewUnSurpportProperty.ToList();
        }
        #endregion
    }

    public sealed class UmcListBox : DefaultConverter<UmcListBox, ListBox>, IListBox
    {

        #region IListBox Members

        public string Name
        {
            get { throw new NotImplementedException(); }
        }

        public bool Enabled
        {
            get { throw new NotImplementedException(); }
        }

        public Size Size
        {
            get { throw new NotImplementedException(); }
        }

        public Point Location
        {
            get { throw new NotImplementedException(); }
        }

        public string Text
        {
            get { throw new NotImplementedException(); }
        }

        public Color BackColor
        {
            get { throw new NotImplementedException(); }
        }

        public Font Font
        {
            get { throw new NotImplementedException(); }
        }

        public Color ForeColor
        {
            get { throw new NotImplementedException(); }
        }

        //public System.Windows.Forms.ListBox.ObjectCollection Items
        //{ 
        //    get { throw new NotImplementedException(); }
        //}

        #endregion

        public override XmlNode[] Serialize(IComponent root, XmlDocument document)
        {
            XmlNode[] nodes = base.Serialize(root, document);

            XmlNode node = nodes.First();
            //
            AppendList(document, node, root as ListBox);

            return nodes;
        }

        public override IComponent DeSerialize(IDesignerHost host, XmlDocument document, XmlNode[] node)
        {

            IComponent component = base.DeSerialize(host, document, node);

            AddList(component as ListBox, node.First());

            return component;
        }

        private void AddList(ListBox listBox, XmlNode node)
        {
            listBox.DisplayMember = "Key";
            foreach (XmlNode item in node.ChildNodes)
            {
                listBox.Items.Add(new KeyValuePair<string, string>(
                    item.Attributes["Text"].Value,
                    item.Attributes["Value"].Value));
            }
        }

        private void AppendList(XmlDocument xml, XmlNode node, ListBox listBox)
        {
            if (listBox.Items.Count == 0)
                return;

            Type itemType = listBox.Items[0].GetType();
            if (itemType == typeof(string))
            {
                foreach (string s in listBox.Items)
                {
                    var item = node.AppendChild(xml.CreateNode(XmlNodeType.Element, "Item", null));
                    item.Attributes.Append(xml.CreateAttribute("Text")).Value = s;
                    item.Attributes.Append(xml.CreateAttribute("Value")).Value = s;
                }
            }
            else
            {
                foreach (KeyValuePair<string, string> s in listBox.Items)
                {
                    var item = node.AppendChild(xml.CreateNode(XmlNodeType.Element, "Item", null));
                    item.Attributes.Append(xml.CreateAttribute("Text")).Value = s.Value;
                    item.Attributes.Append(xml.CreateAttribute("Value")).Value = s.Key;
                }
            }
        }

    }


    public interface IListBox : IUmc
    {
        string Name { get; }
        bool Enabled { get; }
        Size Size { get; }
        Point Location { get; }
        string Text { get; }
        Color BackColor { get; }
        Font Font { get; }
        Color ForeColor { get; }
        //System.Windows.Forms.ListBox.ObjectCollection Items { get; }
    }

}
